package org.pointex.service.impl;

import java.io.Serializable;







import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.pointex.common.service.impl.BaseServiceImpl;
import org.pointex.dao.CreditsRecordDao;
import org.pointex.dao.CreditsRuleDao;
import org.pointex.dao.impl.CreditsRuleDaoImpl;
import org.pointex.domain.CreditsRecord;
import org.pointex.domain.CreditsRule;
import org.pointex.domain.Users;
import org.pointex.service.CreditsRuleService;
import org.pointex.service.UsersService;
import org.pointex.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @ClassName: CreditsRuleServiceImpl  
 * @Description: TODO(积分规则业务实现类)  
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午4:02:01  
 *
 */
@Service("creditsRuleService")
public class CreditsRuleServiceImpl extends BaseServiceImpl<CreditsRule, Serializable> implements CreditsRuleService {
	
	@Autowired
	private CreditsRuleDao creditsRuleDao;
	
	@Autowired
	private UsersService usersService;
	
	@Autowired
	private CreditsRecordDao creditsRecordDao;

	/**
	 * 获取用户的签到天数和不同会员单位定制的签到规则获取积分
	 */
	@Override
	public Integer getCreditsByRule(Users users,String behaviorid) {
		String memberid=users.getMemberId();  //会员编号
		String userid=users.getUserId();     //登录用户编号
		//积分动作编号
		String actionid="CB00001";  
		int result=0;
		try {
			Map param= new HashMap();
			param.put("creditsActionId", actionid);//拿到该动作的所有规则
			param.put("memberId", memberid);
			param.put("status", 1);
			param.put("delFlag", 0);
			param.put("startCreditsDt", DateUtil.formatDate_YYYYMMDD(new Date()));
			param.put("endCreditsDt", DateUtil.formatDate_YYYYMMDD(new Date()));
			
			List<CreditsRule> crules=creditsRuleDao.selectParam(param);
			if(actionid=="CB00001"){//签到
				//redis获取用户编号和用户会员编号以及连续签到天数
				int days=2;
				int weeks=2;
				for (CreditsRule creditsRule : crules) {
					//日签到
					if(creditsRule.getCreditsTypeId().toString().trim().equals("CT00001")){
						//根据条件范围选择规则
						if(days>creditsRule.getMinValue()&&days<=creditsRule.getMaxValue()){
							//判断积分值是否达到上限，达到上限则不进行积分操作
							if(getCreditsOvertop(userid, creditsRule)){
								if(usersService.updateCredits(users,new Double(creditsRule.getCreditsVallue()).intValue() , 1,actionid,creditsRule.getCreditsRuleContent()))
									result +=creditsRule.getCreditsVallue();
							}
						}
					}
					//周签到
					if(creditsRule.getCreditsTypeId().toString().trim().equals("CT00002")){
						if(weeks>creditsRule.getMinValue()&&weeks<=creditsRule.getMaxValue()){
							//判断积分值是否达到上限，达到上限则不进行积分操作
							if(getCreditsOvertop(userid, creditsRule)){
								if(usersService.updateCredits(users,new Double(creditsRule.getCreditsVallue()).intValue() , 1, actionid,creditsRule.getCreditsRuleContent()))
									result +=creditsRule.getCreditsVallue();
							}
						}
					}	
				}		
			}else if(actionid=="CB00002"){//下单
				//获取交易金额以及交易手数
				int money=10;
				int num=20;
				for (CreditsRule creditsRule : crules) {
					//交易手数
					if(creditsRule.getCreditsTypeId().toString().trim().equals("CT00004")){
						//根据条件范围选择规则
						if(num>creditsRule.getMinValue()&&num<=creditsRule.getMaxValue()){
							//判断积分值是否达到上限，达到上限则不进行积分操作
							if(getCreditsOvertop(userid, creditsRule)){
								if(usersService.updateCredits(users,new Double(creditsRule.getCreditsVallue()).intValue() , 1,actionid,creditsRule.getCreditsRuleContent()))
									result +=creditsRule.getCreditsVallue();
							}
						}
					}else if(creditsRule.getCreditsTypeId().toString().trim().equals("CT00005")){//交易金额
						if(money>creditsRule.getMinValue()&&money<=creditsRule.getMaxValue()){
							//判断积分值是否达到上限，达到上限则不进行积分操作
							if(getCreditsOvertop(userid, creditsRule)){
								if(usersService.updateCredits(users, new Double(creditsRule.getCreditsVallue()).intValue() , 1,actionid,creditsRule.getCreditsRuleContent()))
									result +=creditsRule.getCreditsVallue();
							}
						}
					}
				}
			}else if(actionid=="CB00003"){//牛人跟踪
				
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * 用户执行操作是否达到积分上限值
	 * param userid = 用户编号
	 * param startTime = 统计积分开始时间
	 * param endTime = 统计积分结束时间
	 * param creditsRuleId = 积分规则索引号 
	 * param overCredits = 积分上限值
	 * param ruleCredits = 获取积分
	 */
	@Override
	public boolean getCreditsOvertop(String userid,CreditsRule creditsRule) {
		boolean result= false;
		try {
			Map param=new HashMap();
			param.put("userId", userid);
			param.put("creditsRuleId", creditsRule.getCreditsRuleId());
			//是否每天一次性    1是、0否
			if (creditsRule.getIsRepeat()==1) {
				param.put("createDt", DateUtil.formatDate_YYYYMMDD(new Date()));
			}else if(creditsRule.getIsRepeat()==0){
				param.put("startTime", creditsRule.getStartCreditsDt());
				param.put("endTime", creditsRule.getEndCreditsDt());
			}
			
			List<CreditsRecord> creditsRecords= creditsRecordDao.selectParam(param);
			int sumCredits=0;
			for (CreditsRecord creditsRecord : creditsRecords) {
				sumCredits +=creditsRecord.getCreditsValue();
			}
			System.out.println(creditsRule.getCreditsVallue());
			System.out.println(sumCredits+creditsRule.getCreditsVallue());
			//执行操作积分+已获取积分不超过积分上限就返回TRUE，执行下一步操作
			if(sumCredits+creditsRule.getCreditsVallue()<=creditsRule.getCreditsUpperValue())
				result=true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
}
